// pages/studyWord/lastbox/lastbox.js
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

let boxwordlist = [{
  "id": "20114",
  "translate": "超市",
  "package_id": "197",
  "word": "supermarket",
  "create_time": "2018-01-11 16:18:58",
  "american": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/caution.mp3",
  "england": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/caution.mp3",
  "status": "1",
  "in_today": 0
}, {
  "id": "20081",
  "translate": "蓝色的",
  "package_id": "197",
  "word": "blue",
  "create_time": "2018-01-11 16:18:58",
  "american": "",
  "england": "",
  "status": "1",
  "in_today": 0
}];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: "easy", //	type 1 熟词 2 已会 3 难词 type(difficult  complete  easy)
    tabs: ["熟词", "已会", "难词"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    imageurl: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/',
    innerAudioContext: new Object(),
    boxWordList: [],
    language: 1, //0为英音，1为美音
    selNums: {},
    readTimes: {},
    packageid: "",
    page: 1,//分页 第一页是1
    pageSize: 200,
    hasMoreData: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //初始化audio
    that.data.innerAudioContext = wx.createInnerAudioContext();
    that.data.innerAudioContext.autoplay = false;
    that.data.innerAudioContext.loop = false;
    that.data.packageid = options.packageid;
    //获取是英音还是美音
    if (app.globalData.isUsvoice === 0) {
      that.setData({
        language: 0,
      })
    } else {
      that.setData({
        language: 1
      })
    }
    //设置tab
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },

  //点击某个Tab
  tabClick: function (e) {
    var that = this;
    that.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });

    if (that.data.activeIndex == 0) {
      that.setData({
        type: "easy",
        page: 1,
      })
    } else if (that.data.activeIndex == 1) {
      that.setData({
        type: "complete",
        page: 1,
      })
    } else if (that.data.activeIndex == 2) {
      that.setData({
        type: "difficult",
        page: 1,
      })
    }
    console.log("当前type:" + that.data.type)
    //获取各类单词列表
    that.getboxWordList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.setData({
      page: 1,
      boxWordList: [],
    })
    that.getboxWordList();
  },

  // 获取盒子内各类单词的列表
  getboxWordList: function () {
    console.log("调查看某个盒子单词的接口，盒子：" + this.data.type)
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })
    httpUtils.getBoxWords({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      package_id: that.data.packageid,
      type: that.data.type,
      page: that.data.page,
      square_type: 8,
    }).then((res) => {
      var boxWordList = that.data.boxWordList;
      var list = res.data;
      if (that.data.page == 1) {
        boxWordList = []
      }
      if (list.length < that.data.pageSize) {
        that.setData({
          boxWordList: boxWordList.concat(list),
          hasMoreData: false
        })
      } else {
        that.setData({
          boxWordList: boxWordList.concat(list),
          hasMoreData: true,
          page: that.data.page + 1,
        })
      }

      // that.setData({
      //   boxWordList: res.data,
      // })
    });
  },

  //点击单词条目
  clickTxt: function (e) {
    this.data.innerAudioContext.stop();
    let temWrod = e.currentTarget.dataset.words;
    let temNum = this.data.selNums;
    // let audiosrc = app.globalData.voicepath + temWrod + ".mp3";
    let audiosrc=e.currentTarget.dataset.audios;
    this.data.innerAudioContext.src = audiosrc;
    this.data.innerAudioContext.play();

    var isHas = false;//是否点击过 false没有点击过
    for (var key in temNum) {//0正常样式1英文变粗 2英文变粗出中文
      if (key == temWrod) {
        if (temNum[key] < 2) {
          temNum[key]++;
        } else {
          temNum[key] = 1;
        }
        isHas = true;
      } else {
        temNum[key] = 0;
      }
    }
    if (!isHas) {
      temNum[temWrod] = 1;
    }
    this.setData({
      selNums: temNum,
    });
    let audios = e.currentTarget.dataset.audios;
    this.data.innerAudioContext.src = audios;
    this.data.innerAudioContext.play();
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("onReachBottom方法触发了")
    var that = this;
    if (that.data.hasMoreData) {
      that.getboxWordList();
      console.log("请求下一页数据")
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    }
  },
})